package com.leyou.item.web;

import com.leyou.common.dto.BrandDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import com.netflix.ribbon.proxy.annotation.Http;
import org.jboss.logging.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
     @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findBradsByBid(@RequestParam("ids") List<Long> id){
     return ResponseEntity.ok(brandService.findBradsByBid(id));
    }
    //分类id查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findBrandByCid(@RequestParam("id") Long id){
         return ResponseEntity.ok(brandService.findBrandByCid(id));
    }
    //根据id查询品牌
   /* @GetMapping("/{id}")
   public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id") Long id){
         return ResponseEntity.ok(new BrandDTO(this.brandService.getById(id)));
   }*/
   //根据id删除品牌
    @DeleteMapping("/{id}")
     public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
         //删除中间表
        brandService.deleteBrandById(id);
        //删除品牌id
        brandService.removeById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
      //分页查询
      @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> findPagesByHelper(@RequestParam(value = "key",required = false) String key,
                                                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                       @RequestParam(value = "rows",defaultValue = "5") Integer rows){

       return ResponseEntity.ok(brandService.findPagesByHelper(key,page,rows));
      }

      //新增品牌
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO){
         brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //根据id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findBrand(@PathVariable("id") Long id){

        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }
    //更新品牌
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO,@RequestParam("categoryIds") List<Long> categoryIds){
         brandService.updateBrand(brandDTO,categoryIds);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
